<?php

namespace app\model;

//use support\Model;
use app\controller\wmapi\IndexController;
use support\Db;
use Illuminate\Database\Eloquent\Model;

class WaPurchasePayrollRequisition
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_payroll_requisition_purchase';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public static function all($condition)
    {
        $list = Db::table('wa_payroll_requisition_purchase')
            ->select('*')
            ->where($condition)
            ->whereBetween('status', [0, 1])
            ->get();
//        ->toSql();
        if ($list) {
            foreach ($list as $k) {

                if($k->payment_company_info){
                    $k->payment_company_info = json_decode($k->payment_company_info);
                }

                if($k->collecting_company_info){
                    $k->collecting_company_info = json_decode($k->collecting_company_info);
                }

                if ($k->voucher){
                    $k->voucher = json_decode($k->voucher);
                }


                if ($k->payment_voucher) {
                    $k->payment_voucher = json_decode($k->payment_voucher);
                }
                if ($k->status == 0) {
//                    0待审批 1审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
                    $k->status_name = '待审批';
                } elseif ($k->status == 1) {
                    $k->status_name = '审批中';
                } elseif ($k->status == 2) {
                    $k->status_name = '待付款';
                } elseif ($k->status == 3) {
                    $k->status_name = '已付款';
                } elseif ($k->status == 4) {
                    $k->status_name = '审核不通过';
                } elseif ($k->status == 5) {
                    $k->status_name = '撤回';
                } elseif ($k->status == 6) {
                    $k->status_name = '删除';
                } else {
                    $k->status_name = '';
                }


            }
        }
        return json_encode($list);
    }


    public static function allByPur($condition)
    {
        $list = Db::table('wa_payroll_requisition_purchase')
            ->select('*')
            ->where($condition)
//            ->whereBetween('status',[0,2])
            ->get();
//        ->toSql();
        if ($list) {
            foreach ($list as $k) {
                if($k->payment_company_info){
                    $k->payment_company_info = json_decode($k->payment_company_info);
                }

                if($k->collecting_company_info){
                    $k->collecting_company_info = json_decode($k->collecting_company_info);
                }
                if ($k->voucher){
                    $k->voucher = json_decode($k->voucher);
                }

                if ($k->payment_voucher) {
                    $k->payment_voucher = json_decode($k->payment_voucher);
                }

                if ($k->status == 0) {
//                    0待审批 1审批中 2 待付款 3 已付款 4 审核不通过 5 撤回 6 删除
                    $k->status_name = '待审批';
                } elseif ($k->status == 1) {
                    $k->status_name = '审批中';
                } elseif ($k->status == 2) {
                    $k->status_name = '待付款';
                } elseif ($k->status == 3) {
                    $k->status_name = '已付款';
                } elseif ($k->status == 4) {
                    $k->status_name = '审核不通过';
                } elseif ($k->status == 5) {
                    $k->status_name = '已撤回';
                } elseif ($k->status == 6) {
                    $k->status_name = '删除';
                } else {
                    $k->status_name = '';
                }


            }
        }
        return json_encode($list);
    }

    public static function created($data)
    {
        $result = Db::table('wa_payroll_requisition_purchase')->insertGetId($data);
        return $result;
    }

    public static function updated($data, $where)
    {
        $result = Db::table('wa_payroll_requisition_purchase')->where($where)->update($data);
        return $result;
    }

    public static function list($adminId, $page, $pageNum, $whereIn, $fields = ['*'], $condition = [], $between = [])
    {

        $offset = ($page - 1) * $pageNum;
        if ($between) {
            if ($whereIn) {
                $model = Db::table('wa_payroll_requisition_purchase as payroll')
//                    ->leftJoin('wa_orders_purchase_order_amount_detail as amount_detail', 'amount_detail.purchase_order_sn','=','payroll.purchase_order_sn')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('payroll.admin_id', $whereIn)
                    ->whereBetween('payroll.created_at', $between);
            } else {
                $model = Db::table('wa_payroll_requisition_purchase as payroll')
//                    ->leftJoin('wa_orders_purchase_order_amount_detail as amount_detail', 'amount_detail.purchase_order_sn','=','payroll.purchase_order_sn')
                    ->select($fields)
                    ->where($condition)
                    ->whereBetween('payroll.created_at', $between);
            }


        } else {
            if ($whereIn) {
                $model = Db::table('wa_payroll_requisition_purchase as payroll')
//                    ->leftJoin('wa_orders_purchase_order_amount_detail as amount_detail', 'amount_detail.purchase_order_sn','=','payroll.purchase_order_sn')
                    ->select($fields)
                    ->where($condition)
                    ->whereIn('payroll.admin_id', $whereIn);
            } else {
                $model = Db::table('wa_payroll_requisition_purchase as payroll')
//                    ->leftJoin('wa_orders_purchase_order_amount_detail as amount_detail', 'amount_detail.purchase_order_sn','=','payroll.purchase_order_sn')
                    ->select($fields)
                    ->where($condition);
            }

        }
        $count = $model->count();
        $list = $model
            ->orderBy('payroll.id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();

        if ($list) {
            foreach ($list as $k) {

                if($k->payment_company_info){
                    $k->payment_company_info = json_decode($k->payment_company_info);
                }

                if($k->collecting_company_info){
                    $k->collecting_company_info = json_decode($k->collecting_company_info);
                }

                // 供应商类型
//                if ($k->supplier_type == 1){
//                    $k->supplier_type_name = '车辆供应商';
//                }elseif ($k->supplier_type == 2){
//                    $k->supplier_type_name = '上牌供应商';
//                }elseif ($k->supplier_type == 3){
//                    $k->supplier_type_name = '国际货运代理';
//                }elseif ($k->supplier_type == 4){
//                    $k->supplier_type_name = '保险供应商';
//                }elseif ($k->supplier_type == 5){
//                    $k->supplier_type_name = '国内物流';
//                }
                $k->supplier_type_name = IndexController::supplierTypeNameCommon($k->supplier_type);

                // 付款方类型 1 挂靠公司 2 出口公司 3 其它
                if($k->payment_company_type == 1){
                    $k->payment_company_type_name = '挂靠公司';
                }elseif ($k->payment_company_type == 2){
                    $k->payment_company_type_name = '出口公司';
                }elseif ($k->payment_company_type == 3){
                    $k->payment_company_type_name = '其它';
                }



                // 判断是否有审批  根据
                $approve = WaPayrollRequisitionExamineApproveFlow::all(
                    ['purchase_payroll_sn' => $k->purchase_payroll_sn,'pr_no' => $k->pr_no]);
                $approve = json_decode($approve);
                if ($approve) {
                    foreach ($approve as $kpr) {
                        if ($kpr->admin_ids){
                            $kpr->admin_ids = json_decode($kpr->admin_ids);
                        }
                        if ($kpr->status == 3) {
                            $approve_status = 3; // 已驳回
                            $approve_status_name = '已撤回';
                        } elseif ($kpr->status == 2) {
                            $approve_status = 2; // 已驳回
                            $approve_status_name = '已驳回';
                        } elseif ($kpr->status == 1) {
                            if (in_array($adminId->id, $kpr->admin_ids)){
                                $approve_status = 1; // 已审批
                                $approve_status_name = '已审批';
                            }else{
                                $approve_status = 998; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } elseif ($kpr->status == 0) {
                            if (in_array($adminId->id, $kpr->admin_ids)){
                                $approve_status = 0; // 待审批
                                $approve_status_name = '待审批';
                            }else{
                                $approve_status = 998; // 审批中
                                $approve_status_name = '审批中';
                            }

                        } else {
                            $approve_status = 999; // 已驳回
                            $approve_status_name = '不在审批流中';
                        }
                    }
                } else {
                    $approve_status = 10001; // 审批状态
                    $approve_status_name = '没有审批记录';// 审批状态名称
                }
                $k->approve_status = $approve_status; // 审批状态
                $k->approve_status_name = $approve_status_name;// 审批状态名称

                if ($k->voucher){
                    $k->voucher = json_decode($k->voucher);
                }
                if ($k->payment_voucher) {
                    $k->payment_voucher = json_decode($k->payment_voucher);
                }
//                if ($k->payment_company_info) {
//                    $k->payment_company_info = json_decode($k->payment_company_info);
//                }
//                if ($k->collecting_company_info) {
//                    $k->collecting_company_info = json_decode($k->collecting_company_info);
//                }
                if ($k->payment_details) {
                    $k->payment_details = json_decode($k->payment_details);
                }


                // 采购请款单状态 0 草稿(撤回审批)  1 审批中(提交审批) 2 待付款 3 已付款 4 审核不通过 5 删除
                if ($k->status == 0) {
                    $k->status_name = '待审批';
                } elseif ($k->status == 1) {
                    $k->status_name = '审批中';
                } elseif ($k->status == 2) {
                    $k->status_name = '待付款';
                } elseif ($k->status == 3) {
                    $k->status_name = '已付款';
                } elseif ($k->status == 4) {
                    $k->status_name = '审核不通过';
                } elseif ($k->status == 5) {
                    $k->status_name = '删除';
                } else {
                    $k->status_name = '';
                }

            }
        }
        return json_encode(['list' => $list, 'count' => $count]);


    }

    public static function find($condition)
    {

//        $res = Db::table('wa_payroll_requisition_purchase as payroll')
        $res = Db::table('wa_payroll_requisition_purchase')
//                ->leftJoin('wa_orders_purchase as purchase', 'purchase.purchase_order_sn','=','payroll.purchase_order_sn')
            ->where($condition)
            ->first();

        if ($res) {

            if($res->payment_company_info){
                $res->payment_company_info = json_decode($res->payment_company_info);
            }

            if($res->collecting_company_info){
                $res->collecting_company_info = json_decode($res->collecting_company_info);
            }
            // 供应商类型
//            if ($res->supplier_type == 1){
//                $res->supplier_type_name = '车辆供应商';
//            }elseif ($res->supplier_type == 2){
//                $res->supplier_type_name = '上牌供应商';
//            }elseif ($res->supplier_type == 3){
//                $res->supplier_type_name = '国际货运代理';
//            }elseif ($res->supplier_type == 4){
//                $res->supplier_type_name = '保险供应商';
//            }elseif ($res->supplier_type == 5){
//                $res->supplier_type_name = '国内物流';
//            }
            $res->supplier_type_name = IndexController::supplierTypeNameCommon($res->supplier_type);


            // 付款方类型 1 挂靠公司 2 出口公司 3 其它
            if($res->payment_company_type == 1){
                $res->payment_company_type_name = '挂靠公司';
            }elseif ($res->payment_company_type == 2){
                $res->payment_company_type_name = '出口公司';
            }elseif ($res->payment_company_type == 3){
                $res->payment_company_type_name = '其它';
            }



            // 货币类型   1 人民币 2 美元 3 欧元
//            if ($res->currency == 1) {
//                $res->currency_name = '人民币';
//            } elseif ($res->currency == 2) {
//                $res->currency_name = '美元';
//            } elseif ($res->currency == 3) {
//                $res->currency_name = '欧元';
//            } else {
//                $res->currency_name = '';
//            }

            $res->currency_name = IndexController::currencyNameCommon($res->currency);


            if ($res->voucher){
                $res->voucher = json_decode($res->voucher);
            }
            if ($res->payment_voucher) {
                $res->payment_voucher = json_decode($res->payment_voucher);
            }

            if ($res->payment_details) {
                $res->payment_details = json_decode($res->payment_details);
            }

        }
        return json_encode($res);
    }


    public static function findReadyBack($condition)
    {

        $res = Db::table('wa_payroll_requisition_purchase')
            ->where($condition)
            ->first();


        return $res;
    }

    public static function sums($where)
    {
        $result = Db::table('wa_payroll_requisition_purchase')
            ->where($where)
//            ->whereBetween('status', [0,3])
            ->where(['status' => 3]) // 已请款必须是 审批通过的
            ->sum('this_request_price');
        return $result;
    }


    public static function suming($where)
    {
        $result = Db::table('wa_payroll_requisition_purchase')
            ->where($where)
//            ->whereBetween('status', [0,3])
            ->where(['status' => 1]) // 请款中 审批中
            ->sum('this_request_price');
        return $result;
    }


    public static function payrollStatus($where)
    {
        $result = Db::table('wa_payroll_requisition_purchase')
            ->where($where)
            ->whereBetween('status', [0,3])
//            ->where(['status', '<=', 3]) // 已请款必须是 审批通过的
            ->sum('this_request_price');
        return $result;
    }


    /**
     * 删除
     * @param $data
     * @return int
     */
    public static function deleted($condition)
    {
        return Db::table('wa_payroll_requisition_purchase')->where($condition)->delete();
    }


}